<script  lang="ts" setup>

import {computed, onMounted, reactive, ref} from "vue";
import {
  listPictureByPageUsingPost,
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost, listPictureVoByPageWithCacheUsingPost
} from "@/api/pictureController";
import {message} from "ant-design-vue";
import {useRouter} from "vue-router";
import PictureList from "@/components/PictureList.vue";

// 表格数据相关
const dataList = ref<API.Picture[]>([]);
const total = ref<number>(0);
const loading = ref(true);

const categoryList= ref<string[]>([])
const tagList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const selectedTagList = ref<boolean[]>([])

const router = useRouter()

// 分页配置
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: (total: number) => `共 ${total} 条`,
    pageSizeOptions: ['10', '20', '50', '100'],
    onChange: (page: any, pageSize: any) => {
      searchParams.current = page
      searchParams.pageSize = pageSize
      getData()
    }
  }
});

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  pageSize: 12,
  current: 1,
  sortOrder: 'ascend',
  sortField: 'createTime'
});
const getData = async () => {
  const params = {
    ...searchParams,
    tags: [] as string[]
  }
  if (selectedCategory.value !== 'all'){
    params.category = selectedCategory.value
  }
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag){
      params.tags.push(tagList.value[index])
    }
  })
  try {
    loading.value = true;
    const res = await listPictureVoByPageWithCacheUsingPost(params);
    if (res.data.code === 0 && res.data.data) {
      dataList.value = res.data.data.records ?? [];
      total.value = res.data.data.total ?? 0;
    } else {
      message.error('数据获取失败：' + res.data.message);
    }
  } catch (e) {
    message.error('请求异常');
  } finally {
    loading.value = false;
  }
};
const doSearch = () => {
  searchParams.current = 1
  getData()
}
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data){
    tagList.value = res.data.data.tagList ?? []
    categoryList.value = res.data.data.categoryList ?? []
  }else {
    message.error('获取标签列表失败')
  }
}
onMounted(() => {
  getTagCategoryOptions()
  getData()
})
</script>

<template>
  <div class="picture-home-container">
    <!-- 搜索区域 -->
    <div class="hero-section">
      <div class="search-wrapper">
        <div class="search-glow"></div>
        <h1 class="hero-title">探索视觉创意宇宙</h1>
        <a-input-search
            v-model:value="searchParams.searchText"
            placeholder="输入关键词探索海量图片..."
            enter-button="立即搜索"
            size="large"
            @search="doSearch"
            class="neo-search"
        >
          <template #prefix>
            <div class="search-icon">
              <svg class="icon" viewBox="0 0 24 24">
                <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
              </svg>
            </div>
          </template>
        </a-input-search>
      </div>
    </div>

    <!-- 分类筛选 -->
    <div class="category-filter">
      <div class="filter-scroll">
        <div
            v-for="category in ['all', ...categoryList]"
            :key="category"
            :class="['category-item', { active: selectedCategory === category }]"
            @click="selectedCategory = category; doSearch()"
        >
          {{ category === 'all' ? '全部' : category }}
          <div class="active-bar"></div>
        </div>
      </div>
    </div>

    <!-- 标签筛选 -->
    <div class="tag-filter">
      <div class="tag-scroll">
        <div
            v-for="(tag, index) in tagList"
            :key="tag"
            :class="['tag-item', { active: selectedTagList[index] }]"
            @click="selectedTagList[index] = !selectedTagList[index]; doSearch()"
        >
          # {{ tag }}
        </div>
      </div>
    </div>

    <!-- 图片列表 -->
    <PictureList :data-list="dataList" :loading="loading" :pagination="pagination"></PictureList>
  </div>
</template>

<style scoped lang="less">
@primary-color: #6366f1;
@bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);

.picture-home-container {
  padding: 0 2rem;
  background: @bg-gradient;
  min-height: 100vh;
}

.hero-section {
  position: relative;
  padding: 4rem 0;
  margin-bottom: 2rem;
  overflow: hidden;

  .search-wrapper {
    max-width: 800px;
    margin: 0 auto;
    position: relative;
    z-index: 1;
  }

  .search-glow {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 150%;
    height: 150%;
    background: radial-gradient(circle, rgba(99,102,241,0.2) 0%, transparent 60%);
    transform: translate(-50%, -50%);
  }

  .hero-title {
    color: #e2e8f0;
    font-size: 2.5rem;
    text-align: center;
    margin-bottom: 2rem;
    text-shadow: 0 4px 12px rgba(0,0,0,0.3);
  }
}

.neo-search {
  :deep(.ant-input-search-button) {
    background: linear-gradient(45deg, @primary-color, #8b5cf6);
    border: none;
    height: 48px;
    padding: 0 2rem;
    font-weight: 500;
    transition: all 0.3s ease;

    &:hover {
      transform: scale(1.02);
      box-shadow: 0 4px 15px rgba(@primary-color, 0.3);
    }
  }

  :deep(.ant-input) {
    height: 48px;
    background: rgba(15, 23, 42, 0.8);
    border: 1px solid rgba(99,102,241,0.3);
    color: #e2e8f0;
    border-radius: 12px;
    padding-left: 3rem;

    &:focus {
      box-shadow: 0 0 0 2px rgba(@primary-color, 0.2);
    }
  }

  .search-icon {
    position: absolute;
    left: 1rem;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;

    .icon {
      width: 24px;
      height: 24px;
      fill: #818cf8;
    }
  }
}

.category-filter {
  margin: 2rem 0;
  overflow-x: auto;

  .filter-scroll {
    display: flex;
    gap: 1rem;
    padding-bottom: 1rem;
  }

  .category-item {
    padding: 0.8rem 1.5rem;
    border-radius: 8px;
    background: rgba(15, 23, 42, 0.6);
    color: #94a3b8;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    white-space: nowrap;

    &.active {
      background: @primary-color;
      color: white;
      font-weight: 500;

      .active-bar {
        width: 100%;
      }
    }

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(@primary-color, 0.2);
    }
  }
}

.tag-filter {
  margin-bottom: 2rem;
  overflow-x: auto;

  .tag-scroll {
    display: flex;
    gap: 0.8rem;
    padding-bottom: 1rem;
  }

  .tag-item {
    padding: 0.6rem 1.2rem;
    border-radius: 20px;
    background: rgba(99,102,241,0.15);
    color: #818cf8;
    cursor: pointer;
    transition: all 0.3s ease;

    &.active {
      background: @primary-color;
      color: white;
      transform: scale(1.05);
    }

    &:hover {
      background: rgba(99,102,241,0.3);
    }
  }
}

.picture-gallery {
  padding-bottom: 4rem;

  .picture-card-wrapper {
    padding: 8px;
  }

  .picture-card {
    background: rgba(15, 23, 42, 0.8);
    border-radius: 16px;
    overflow: hidden;
    position: relative;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;

    &:hover {
      transform: translateY(-8px);
      box-shadow: 0 12px 24px rgba(0,0,0,0.3);

      .card-hover {
        opacity: 1;
      }

      .picture-image {
        transform: scale(1.05);
      }
    }

    .card-hover {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0,0,0,0.5);
      display: flex;
      align-items: center;
      justify-content: center;
      opacity: 0;
      transition: opacity 0.3s ease;

      .view-text {
        color: white;
        font-size: 1.2rem;
        text-shadow: 0 2px 4px rgba(0,0,0,0.3);
      }
    }

    .picture-image {
      width: 100%;
      height: 240px;
      object-fit: cover;
      transition: transform 0.3s ease;
    }

    .card-content {
      padding: 1.2rem;

      .picture-title {
        color: #e2e8f0;
        margin-bottom: 0.8rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .picture-meta {
        display: flex;
        gap: 1rem;
        margin-bottom: 1rem;
        color: #94a3b8;
        font-size: 0.9rem;

        .meta-icon {
          margin-right: 4px;
        }
      }

      .picture-tags {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;

        .category-tag {
          background: rgba(@primary-color, 0.15);
          color: @primary-color;
          padding: 4px 8px;
          border-radius: 4px;
        }

        .tag {
          background: rgba(255,255,255,0.1);
          color: #cbd5e1;
          padding: 4px 8px;
          border-radius: 4px;
          font-size: 0.9rem;
        }
      }
    }
  }
}

.loading-state {
  padding: 4rem 0;
  text-align: center;

  .loading-wave {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-bottom: 1rem;

    .wave {
      width: 4px;
      height: 24px;
      background: @primary-color;
      animation: wave 1s ease-in-out infinite;

      &:nth-child(2) { animation-delay: 0.2s; }
      &:nth-child(3) { animation-delay: 0.4s; }
    }
  }

  .loading-text {
    color: #94a3b8;
  }
}

@keyframes wave {
  0%, 100% { transform: scaleY(1); }
  50% { transform: scaleY(1.5); }
}

@media (max-width: 768px) {
  .hero-section {
    padding: 2rem 0;

    .hero-title {
      font-size: 1.8rem;
    }
  }

  .category-filter .category-item {
    padding: 0.6rem 1rem;
  }

  .picture-card .picture-image {
    height: 180px;
  }
}
</style>
